﻿using Signit.Sdk.Entity;
using Signit.Sdk.Http;
using Signit.Sdk.Types;
using System.Collections.Generic;

namespace SdkSample.Testing
{
    class TestQuickSign : TestBase
    {
        public override void Test()
        {
            var fileUrl = "https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/quickSignature.txt";

            var fileData = new FileData
            {
                Url = fileUrl,
                EnableSinglePage = true
            };

            List<Signer> signDetails = new List<Signer>
            {
                setSignerForWriteSignWithKeyword(),
                setSignerForWriteSignWithRectangle(),
                setSignerForSealSignWithKeyword()
            };

            var signRequest = new SignatureRequest
            {
                AcceptDataType = AcceptDataType.URL,
                FileData = fileData,
                SignDetails = signDetails,
                ReturnUrl = "https://webhook.site/b8faacef-e4f6-447a-9d65-90d3a7140844",
                CustomTag = "hello world"
            };

            var result = client.QuickSign(signRequest);
            Log("Quick Sign", result);
        }


        // 使用关键字定位手写签名位置
        private static Signer setSignerForWriteSignWithKeyword()
        {
            // 确定签名数据--这里使用的是手写签名数据
            var writeData = new SealData
            {
                Url = "https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/zhangsan.png",
            };
            var data = new SignerData
            {
                WriteData = writeData,
            };

            // 确定签名位置
            var keyworkPosition = new KeywordPosition
            {
                RelativeWidthRatio = 0.3f,
                RelativeHeightRatio = 1.2f,
                Direction = Direction.RIGHT,
                RelativeOffsetRatio = 0.12f,
                Keyword = "甲代表人（签字）:",
                Scale = 1.5f,
                Pages = "last"
            };

            SignerPosition position = new SignerPosition
            {
                KeywordPosition = keyworkPosition,
            };

            // 设置签名人信息--用于颁发证书
            SignerInfo signerInfo = new SignerInfo
            {
                Contact = "12345678@qq.com",
                Name = "张三",
            };

            // 完整的一个签署信息
            Signer signer = new Signer
            {
                Sequence = 1,
                Data = data,
                Position = position,
                SignerInfo = signerInfo,
            };

            return signer;
        }

        // 直接设置手写签名位置
        private static Signer setSignerForWriteSignWithRectangle()
        {
            // 确定签名数据--这里使用的是手写签名数据
            var writeData = new SealData
            {
                Url = "https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/lisi.png",
            };

            var data = new SignerData
            {
                WriteData = writeData,
            };

            // 确定签名位置
            // 设置小技巧：针对pdf的文档：ulx,uly：使用截图工具从文档左上角顶点拉到签名框右上角，得到截图大小，然后*0.75，对lrx和lry同理
            var rectPosition = new RectanglePosition
            {
                Lrx = 400 * 0.75f,
                Lry = 630 * 0.75f,
                Ulx = 320 * 0.75f,
                Uly = 600 * 0.75f,
                Scale = 1f,
                Page = 1,
            };

            var position = new SignerPosition
            {
                RectanglePosition = rectPosition,
            };

            // 设置签名人信息--用于颁发证书
            var signerInfo = new SignerInfo
            {
                Contact = "12345678@qq.com",
                Name = "李四",
            };
            // 完整的一个签署信息
            var signer = new Signer
            {
                Sequence = 1,
                Data = data,
                Position = position,
                SignerInfo = signerInfo,
            };

            return signer;

        }

        // 使用表单域域名定位手写签名位置--请确保签名文件格式为pdf且拥有对应域名的签名域
        // 利用签名域的测试文件：https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/quick_signature_with_fieldname.pdf
        private static Signer setSignerForWriteSignWithFieldname()
        {
            // 确定签名数据--这里使用的是手写签名数据
            var writeData = new SealData
            {
                Url = "https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/wangwu.png",
            };
            var data = new SignerData
            {
                WriteData = writeData,
            };
            // 确定签名位置
            var position = new SignerPosition
            {
                FieldName = "王五签名",
            };
            // 设置签名人信息--用于颁发证书
            SignerInfo signerInfo = new SignerInfo
            {
                Contact = "12345678@qq.com",
                Name = "王五",
            };
            // 完整的一个签署信息
            Signer signer = new Signer
            {
                Sequence = 1,
                Data = data,
                Position = position,
                SignerInfo = signerInfo,
            };

            return signer;
        }

        // 使用关键字定位签章位置
        private static Signer setSignerForSealSignWithKeyword()
        {
            // 确定签名数据--这里使用的是签章数据
            SealData sealData = new SealData
            {
                Url = "https://raw.githubusercontent.com/signit-wesign/java-sdk-sample/master/demoData/testSeal.png",
            };
            var data = new SignerData
            {
                SealData = sealData,
            };

            // 确定签名位置
            var keywordPosition = new KeywordPosition
            {
                Width = 75f,
                Height = 75f,
                Direction = Direction.CENTER,
                Keyword = "甲方盖章处",
                Scale = 1f,
                Pages = "last"
            };

            var position = new SignerPosition
            {
                KeywordPosition = keywordPosition,
            };

            // 设置签名人信息--用于颁发证书
            var signerInfo = new SignerInfo
            {
                Contact = "12345678@qq.com",
                Name = "赵六",
            };
            // 完整的一个签署信息
            var signer = new Signer
            {
                Sequence = 3,
                Data = data,
                Position = position,
                SignerInfo = signerInfo,
            };

            return signer;
        }
    }
}
